local sk__nizhan = fk.CreateSkill {

  name = "sk__nizhan",

  tags = { Skill.Compulsory, },

}



sk__nizhan:addEffect(fk.EventPhaseStart, {
  name = "sk__nizhan",
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(sk__nizhan.name) and target.phase == Player.Start and not target.dead and
      (target:isWounded() or target:getMark("@sk__nizhan") > 0)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if target:isWounded() then
      room:addPlayerMark(target, "@sk__nizhan", 1)
    else
      room:removePlayerMark(target, "@sk__nizhan", 1)
    end
  end,
})
sk__nizhan:addEffect(fk.EventLoseSkill, {
  can_refresh = function (self, event, target, player, data)
    if target == player and not table.find(player.room:getOtherPlayers(player), function(p)
      return p:hasSkill("sk__nizhan", true, true)
    end) then
      if event == fk.EventLoseSkill then
        return data.skill.name == sk__nizhan.name
      else
        return player:hasSkill("sk__nizhan", true, true)
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getAlivePlayers()) do
      room:setPlayerMark(p, "@sk__nizhan", 0)
    end
  end,
})
sk__nizhan:addEffect(fk.Deathed, {
  can_refresh = function (self, event, target, player, data)
    if target == player and not table.find(player.room:getOtherPlayers(player), function(p)
      return p:hasSkill("sk__nizhan", true, true)
    end) then
      if event == fk.EventLoseSkill then
        return data == self
      else
        return player:hasSkill("sk__nizhan", true, true)
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getAlivePlayers()) do
      room:setPlayerMark(p, "@sk__nizhan", 0)
    end
  end,
})

return sk__nizhan